package com.dekarrin.scraton;

import com.dekarrin.util.StringWrapper;
import com.dekarrin.util.StringPadder;

public class DialogLine extends ScriptInstruction {

	private String line;
	
	private Author author;
	
	private String[] attributes;
	
	public DialogLine(long timestamp, ScriptFormat format, String line, Author author, String[] attributes) {
		super(timestamp, format);
		this.line = line;
		this.author = author;
		this.attributes = attributes;
		if (format.useQuotes) {
			this.line = "\"" + this.line + "\"";
		}
	}
	
	public String toString() {
		StringBuilder builder = new StringBuilder();
		StringPadder marginPadder = new StringPadder(format.leftMarginWidth, " ");
		String clearMargin = getClearLeftMargin();
		if (format.speakerInMargin) {
			builder.append(marginPadder.padRight(getAuthorName() + ":"));
		} else {
			StringPadder padder = new StringPadder(format.scriptWidth, " ");
			builder.append(padder.center(getAuthorName() + ":"));
			builder.append("\n");
			builder.append(clearMargin);
		}
		int dialogWidth = format.scriptWidth - (format.leftMarginWidth + format.rightMarginWidth);
		StringWrapper wrapper = new StringWrapper(dialogWidth, "\n" + clearMargin);
		builder.append(wrapper.wrap(line));
		return builder.toString();
	}
	
	private String getAuthorName() {
		String name = null;
		switch (format.authorLength) {
			case INITIALS:
			name = author.getInitials();
			break;
			
			case FIRST_NAME_LAST_INITIAL:
			name = author.getFirstNameLastInitial();
			break;
			
			case FIRST_INITIAL_LAST_NAME:
			name = author.getFirstInitialLastName();
			break;
			
			case SHORT_NAME:
			name = author.getShortName();
			break;
			
			case FULL_NAME:
			name = author.getFullName();
			break;
		}
		return name.toUpperCase();
	}

}